home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / PlugIn.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  7.6 KB  |  277 lines

  1. // PlugIn.cpp: implementation of the CPlugIn class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "stdafx.h"
  6. #include "Radiant.h"
  7. #include "PlugIn.h"
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14.  
  15. //////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17. //////////////////////////////////////////////////////////////////////
  18.  
  19. CPlugIn::CPlugIn()
  20. {
  21.   m_hDLL = NULL;
  22.   m_pQERPlugEntitiesFactory = NULL;
  23. }
  24.  
  25. CPlugIn::~CPlugIn()
  26. {
  27.     if (m_pQERPlugEntitiesFactory)
  28.         delete m_pQERPlugEntitiesFactory;
  29.     if (m_hDLL != NULL)
  30.         free();
  31. }
  32.  
  33. bool CPlugIn::load(const char *p)
  34. {
  35.     m_hDLL = ::LoadLibrary(p);
  36.     if (m_hDLL != NULL)
  37.     {
  38.         m_pfnInit = reinterpret_cast<PFN_QERPLUG_INIT>(::GetProcAddress(m_hDLL, QERPLUG_INIT));
  39.         if (m_pfnInit != NULL)
  40.         {
  41.             m_strVersion = (*m_pfnInit)(AfxGetApp()->m_hInstance, g_pParentWnd->GetSafeHwnd());
  42.             Sys_Printf("Loaded plugin > %s\n", m_strVersion);
  43.             
  44.             m_pfnGetName = reinterpret_cast<PFN_QERPLUG_GETNAME>(::GetProcAddress(m_hDLL, QERPLUG_GETNAME));
  45.             if (m_pfnGetName != NULL)
  46.             {
  47.                 m_strName = (*m_pfnGetName)();
  48.             }
  49.             
  50.             m_pfnGetCommandList = reinterpret_cast<PFN_QERPLUG_GETCOMMANDLIST>(::GetProcAddress(m_hDLL, QERPLUG_GETCOMMANDLIST));
  51.             if (m_pfnGetCommandList)
  52.             {
  53.                 CString str = (*m_pfnGetCommandList)();
  54.                 char cTemp[1024];
  55.                 strcpy(cTemp, str);
  56.                 char* token = strtok(cTemp, ",;");
  57.                 if (token && *token == ' ')
  58.                 {
  59.                     while (*token == ' ')
  60.                         token++;
  61.                 }
  62.                 while (token != NULL)
  63.                 {
  64.                     m_CommandStrings.Add(token);
  65.                     token = strtok(NULL, ",;");
  66.                 }
  67.             }
  68.             
  69.             m_pfnDispatch = reinterpret_cast<PFN_QERPLUG_DISPATCH>(::GetProcAddress(m_hDLL, QERPLUG_DISPATCH));
  70.             m_pfnGetFuncTable = reinterpret_cast<PFN_QERPLUG_GETFUNCTABLE>(::GetProcAddress(m_hDLL, QERPLUG_GETFUNCTABLE));
  71.             
  72.             m_pfnGetTextureInfo = reinterpret_cast<PFN_QERPLUG_GETTEXTUREINFO>(::GetProcAddress(m_hDLL, QERPLUG_GETTEXTUREINFO));
  73.             m_pfnLoadTexture = reinterpret_cast<PFN_QERPLUG_LOADTEXTURE>(::GetProcAddress(m_hDLL, QERPLUG_LOADTEXTURE));
  74.             
  75.             m_pfnGetSurfaceFlags = reinterpret_cast<PFN_QERPLUG_GETSURFACEFLAGS>(::GetProcAddress(m_hDLL, QERPLUG_GETSURFACEFLAGS));
  76.             
  77.             m_pfnRegisterPluginEntities = reinterpret_cast<PFN_QERPLUG_REGISTERPLUGINENTITIES>(::GetProcAddress(m_hDLL, QERPLUG_REGISTERPLUGINENTITIES));
  78.             
  79.             m_pfnInitSurfaceProperties = reinterpret_cast<PFN_QERPLUG_INITSURFACEPROPERTIES>(::GetProcAddress(m_hDLL, QERPLUG_INITSURFACEPROPERTIES));
  80.             
  81.             m_pfnRequestInterface = reinterpret_cast<PFN_QERPLUG_REQUESTINTERFACE>(::GetProcAddress(m_hDLL, QERPLUG_REQUESTINTERFACE));
  82.  
  83.             return (m_pfnDispatch != NULL && m_pfnGetFuncTable != NULL);
  84.             //--return true;
  85.         }
  86.         Sys_Printf("FAILED to Load plugin > %s\n", p);
  87.     }
  88.     LPVOID lpMsgBuf;
  89.     FormatMessage( 
  90.         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
  91.         FORMAT_MESSAGE_FROM_SYSTEM | 
  92.         FORMAT_MESSAGE_IGNORE_INSERTS,
  93.         NULL,
  94.         GetLastError(),
  95.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  96.         (LPTSTR) &lpMsgBuf,
  97.         0,
  98.         NULL 
  99.     );
  100.     Sys_Printf("LoadLibrary failed on %s: GetLastError: %s\n", p, lpMsgBuf );
  101.     // Free the buffer.
  102.     LocalFree( lpMsgBuf );
  103.     free();
  104.     return false;
  105. }
  106.  
  107. _QERTextureInfo* CPlugIn::getTextureInfo()
  108. {
  109.   if (m_pfnGetTextureInfo != NULL)
  110.   {
  111.     return reinterpret_cast<_QERTextureInfo*>((*m_pfnGetTextureInfo)());
  112.   }
  113.   return NULL;
  114. }
  115.  
  116. void CPlugIn::loadTexture(LPCSTR pFilename)
  117. {
  118.   if (m_pfnLoadTexture != NULL)
  119.   {
  120.     (*m_pfnLoadTexture)(pFilename);
  121.   }
  122. }
  123.  
  124. LPVOID CPlugIn::getSurfaceFlags()
  125. {
  126.   if (m_pfnGetSurfaceFlags != NULL)
  127.   {
  128.     return reinterpret_cast<LPVOID>((*m_pfnGetSurfaceFlags)());
  129.   }
  130.   return NULL;
  131. }
  132.  
  133. void CPlugIn::free()
  134. {
  135.   if (m_hDLL != NULL)
  136.     ::FreeLibrary(m_hDLL);
  137.   m_hDLL = NULL;
  138. }
  139.  
  140. const char* CPlugIn::getVersionStr()
  141. {
  142.     return m_pfnGetName();
  143. }
  144.  
  145. const char* CPlugIn::getMenuName()
  146. {
  147.   return m_strName;
  148. }
  149.  
  150. int CPlugIn::getCommandCount()
  151. {
  152.   return m_CommandStrings.GetSize();
  153. }
  154.  
  155. const char* CPlugIn::getCommand(int n)
  156. {
  157.   return m_CommandStrings.GetAt(n);
  158. }
  159.  
  160. void CPlugIn::dispatchCommand(const char* p, vec3_t vMin, vec3_t vMax, BOOL bSingleBrush)
  161. {
  162.   if (m_pfnDispatch)
  163.   {
  164.     (*m_pfnDispatch)(p, vMin, vMax, bSingleBrush);
  165.   }
  166. }
  167.  
  168. void CPlugIn::addMenuID(int n)
  169. {
  170.   m_CommandIDs.Add(n);
  171. }
  172.  
  173. bool CPlugIn::ownsCommandID(int n)
  174. {
  175.   for (int i = 0; i < m_CommandIDs.GetSize(); i++)
  176.   {
  177.     if (m_CommandIDs.GetAt(i) == n)
  178.       return true;
  179.   }
  180.   return false;
  181. }
  182.  
  183. void* CPlugIn::getFuncTable()
  184. {
  185.   if (m_pfnGetFuncTable)
  186.   {
  187.     return (*m_pfnGetFuncTable)();
  188.   }
  189.   return NULL;
  190. }
  191.  
  192. void CPlugIn::RegisterPluginEntities()
  193. {
  194.     // if we found the QERPlug_RegisterPluginEntities export, it means this plugin provides his own entities
  195.     if (m_pfnRegisterPluginEntities)
  196.     {
  197.         // resquest a _QERPlugEntitiesFactory
  198.         if (m_pfnRequestInterface)
  199.         {
  200.             m_pQERPlugEntitiesFactory = new _QERPlugEntitiesFactory;
  201.             m_pQERPlugEntitiesFactory->m_nSize = sizeof(_QERPlugEntitiesFactory);
  202.             if (m_pfnRequestInterface( QERPlugEntitiesFactory_GUID, m_pQERPlugEntitiesFactory ))
  203.             {
  204.                 // create an IEpair interface for the project settings
  205.                 CEpairsWrapper *pProjectEp = new CEpairsWrapper( g_qeglobals.d_project_entity );
  206.                 m_pfnRegisterPluginEntities( pProjectEp );
  207.             }
  208.             else
  209.                 Sys_Printf( "WARNING: failed to request QERPlugEntitiesFactory from plugin %s\n", m_strName.GetBuffer(0) );
  210.         }
  211.         else
  212.             Sys_Printf( "WARNING: QERPlug_RequestInterface not found in %s\n", m_strName.GetBuffer(0) );
  213.     }
  214. }
  215.  
  216. void CPlugIn::InitBSPFrontendPlugin()
  217. {
  218.     if (m_pfnRequestInterface)
  219.     {
  220.         // request a _QERPlugBSPFrontendTable
  221.         g_BSPFrontendTable.m_nSize = sizeof( _QERPlugBSPFrontendTable );
  222.         if ( m_pfnRequestInterface( QERPlugBSPFrontendTable_GUID, &g_BSPFrontendTable ) )
  223.         {
  224.             g_qeglobals.bBSPFrontendPlugin = true;
  225.         }
  226.     }
  227. }
  228.  
  229. void CPlugIn::InitSurfacePlugin()
  230. {
  231.     // if we found the QERPlug_InitSurfaceProperties export, it means this plugin does surface properties
  232.     if (m_pfnInitSurfaceProperties)
  233.     {
  234.         if (g_qeglobals.bSurfacePropertiesPlugin)
  235.         {
  236.             Sys_Printf( "WARNING: conflict for surface properties plugins. %s ignored.\n", m_strName );
  237.             return;
  238.         }
  239.         if (m_pfnRequestInterface)
  240.         {
  241.             // call the plugin surface properties init
  242.             m_pfnInitSurfaceProperties();
  243.             // request filling of the global _QERPlugSurfaceTable
  244.             g_SurfaceTable.m_nSize = sizeof( g_SurfaceTable );
  245.             if ( m_pfnRequestInterface( QERPlugSurfaceTable_GUID, &g_SurfaceTable ) )
  246.             {
  247.                 // update the global so we know we have a surface properties plugin
  248.                 g_qeglobals.bSurfacePropertiesPlugin = true;
  249.             }
  250.             else
  251.                 Sys_Printf( "WARNING: _QERPlugSurfaceTable interface request failed for surface plugin\n" );
  252.         }
  253.         else
  254.             Sys_Printf("WARNING: QERPlug_RequestInterface export not found in surface properties plugin.\n");
  255.     }
  256. }
  257.  
  258. // create a plugin entity
  259. // e is the entity being created
  260. // e->eclass is the plugin eclass info
  261. // e->epairs will be accessed by the plugin entity through a IEpair interface
  262. IPluginEntity * CPlugIn::CreatePluginEntity(entity_t *e)
  263. {
  264.     if (m_pQERPlugEntitiesFactory)
  265.     {
  266.         // create an IEpair interface for e->epairs
  267.         CEpairsWrapper *pEp = new CEpairsWrapper( e );
  268.         IPluginEntity *pEnt = m_pQERPlugEntitiesFactory->m_pfnCreateEntity( e->eclass, pEp );
  269.         if ( pEnt )
  270.             return pEnt;
  271.         delete pEp;
  272.         return NULL;
  273.     }
  274.     Sys_Printf("WARNING: unexpected m_pQERPlugEntitiesFactory is NULL in CPlugin::CreatePluginEntity\n");
  275.     return NULL;
  276. }
  277.